############################################################################
# SCIENCE ARTICLE
# REPLICATION CODE, CHOROPLETH MAPS
# JULY 15, 2016
############################################################################
ipak <- function(pkg){
    new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
    if (length(new.pkg)) 
        install.packages(new.pkg, dependencies = TRUE)
    sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("maptools", "mapproj", "rgeos", "rgdal", "ggplot2", "RColorBrewer","stringr","scales","foreign")
ipak(packages)

library(maptools)
library(mapproj)
library(rgeos)
library(rgdal)
library(ggplot2)
library(RColorBrewer)
library(stringr)
library(scales)
library(foreign)
library(ineq)

#========================================================
# Step 1: Set up the base map of the United States
#========================================================

theme_map <- function(base_size=9, base_family="") {
    require(grid)
    theme_bw(base_size=base_size, base_family=base_family) %+replace%
    theme(axis.line=element_blank(),
          axis.text=element_blank(),
          axis.ticks=element_blank(),
          axis.title=element_blank(),
          panel.background=element_blank(),
          panel.border=element_blank(),
          panel.grid=element_blank(),
          panel.margin=unit(0, "lines"),
          plot.background=element_blank(),
          legend.justification = c(0,0),
          legend.position = c(0,0)
          )
}

## Read U.S. counties moderately-simplified GeoJSON file
us.counties <- readOGR(dsn="gz_2010_us_050_00_5m.json",
                       layer="OGRGeoJSON")

# Convert it to Albers equal area
us.counties.aea <- spTransform(us.counties,
                               CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))

us.counties.aea@data$id <- rownames(us.counties.aea@data)

# Extract, then rotate, shrink & move alaska (and reset projection)
# need to use state IDs via # https://www.census.gov/geo/reference/ansi_statetables.html
alaska <- us.counties.aea[us.counties.aea$STATE=="02",]
alaska <- elide(alaska, rotate=-50)
alaska <- elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 2.3)
alaska <- elide(alaska, shift=c(-2100000, -2500000))
proj4string(alaska) <- proj4string(us.counties.aea)

# extract, then rotate & shift hawaii
hawaii <- us.counties.aea[us.counties.aea$STATE=="15",]
hawaii <- elide(hawaii, rotate=-35)
hawaii <- elide(hawaii, shift=c(5400000, -1400000))
proj4string(hawaii) <- proj4string(us.counties.aea)

# remove old states and put new ones back in; note the different order
# we're also removing puerto rico 
us.counties.aea <- us.counties.aea[!us.counties.aea$STATE %in% c("02", "15", "72"),]
us.counties.aea <- rbind(us.counties.aea, alaska, hawaii)

#========================================================
# Step 2: Merge county-level dataset with base map
#========================================================

state.data <- read.csv("state-data-statabs-2012.csv", header=TRUE)

county.names <- read.csv("fips-by-state.csv", header=TRUE)
county.names$fips  <- as.character(county.names$fips)
ind <- county.names$fips<10000
county.names$id[ind] <- paste("0", county.names$id[ind], sep="")
county.names$id[county.names$id=="00"] <- "0"

county.data <- read.csv("maps_matched.csv", header=TRUE, sep=",")
colnames(county.data) = c("fips", "amount", "num_patents", "num_citations", "num_campaigns","num_successful","successful_100","vc_count","vc_amount", "total_ks_amount", "total_vc_amount", "cf_patents", "vc_patents", "cf_cites", "vc_cites","prop")
county.data = merge(county.names, county.data, by.x="fips", by.y="fips", all.x=TRUE)
county.data$fips <- as.numeric(county.data$fips)
ind <- county.data$fips<10000
county.data$id <- as.character(county.data$fips)
county.data$id[ind] <- paste("0", county.data$id[ind], sep="")
county.data$id[county.data$id=="00"] <- "0"

# replace NA data with zeros for counties with no capital
county.data$amount <- ifelse(is.na(county.data$amount), 0, county.data$amount)
county.data$num_patents <- ifelse(is.na(county.data$num_patents), 0, county.data$num_patents)
county.data$num_citations <- ifelse(is.na(county.data$num_citations), 0, county.data$num_citations)
county.data$num_campaigns <- ifelse(is.na(county.data$num_campaigns), 0, county.data$num_campaigns)
county.data$num_successful <- ifelse(is.na(county.data$num_successful), 0, county.data$num_successful)
county.data$successful_100 <- ifelse(is.na(county.data$successful_100), 0, county.data$successful_100)
county.data$vc_count <- ifelse(is.na(county.data$vc_count), 0, county.data$vc_count)
county.data$vc_amount <- ifelse(is.na(county.data$vc_amount), 0, county.data$vc_amount)
county.data$total_ks_amount <- ifelse(is.na(county.data$total_ks_amount), 0, county.data$total_ks_amount)
county.data$total_vc_amount <- ifelse(is.na(county.data$total_vc_amount), 0, county.data$total_vc_amount)
county.data$cf_patents <- ifelse(is.na(county.data$cf_patents), NA, county.data$cf_patents)
county.data$vc_patents <- ifelse(is.na(county.data$vc_patents), NA, county.data$vc_patents)
county.data$cf_cites <- ifelse(is.na(county.data$cf_cites), NA, county.data$cf_cites)
county.data$vc_cites <- ifelse(is.na(county.data$vc_cites), NA, county.data$vc_cites)
county.data$prop <- ifelse(county.data$prop==0, NA, county.data$prop)

ind <- match(county.data$fips, county.names$fips)
county.data$name <- county.names$name[ind]
county.data$state <- county.names$state[ind]

ind <- match(state.data$State.Abbr, county.data$state)
county.data$state[ind] <- state.data$State.Abbr

## Add state names as levels of county name, so states have FIPS too
levels(county.data$name) <- c(levels(county.data$name), levels(state.data$State))
county.data$name[ind] <- state.data$State

### Add census region. Don't call the variable "region" because that's
### already reserved by the map object
ind <- match(county.data$state, state.data$State.Abbr)
county.data$census.region <- state.data$Region[ind]

library(Hmisc)
county.data$num_campaigns <- cut2(county.data$num_campaigns,
                             cuts = c(0, 1, 10, 100, 1000))
county.data$num_successful <- cut2(county.data$num_successful,
                             cuts = c(0, 1, 10, 100, 1000))
county.data$successful_100 <- cut2(county.data$successful_100,
                             cuts = c(0, 1, 10, 100, 1000))                             
county.data$vc_count <- cut2(county.data$vc_count,
                             cuts = c(0, 1, 10, 100, 1000))
county.data$prop <- cut2(county.data$prop,
#"No VC", "0 < Ratio < 0.5", "0.5 < Ratio < 0.9","1 < Ratio < 1.1", "1.1 < Ratio < 2 ", "2 < Ratio"
                             cuts = c(0,.5, 1, 1.1, 2))     
county.data$cf_patents <- cut2(county.data$cf_patents,
                             cuts = c(0, 100, 1000, 10000, 10000000)) 
county.data$vc_patents <- cut2(county.data$vc_patents,
                             cuts = c(0, 100, 1000, 10000, 10000000))  
county.data$cf_cites <- cut2(county.data$cf_cites,
                             cuts = c(0, 100, 1000, 10000, 10000000)) 
county.data$vc_cites <- cut2(county.data$vc_cites,
                             cuts = c(0, 100, 1000, 10000, 10000000))                                                                                                           
co.map <- fortify(us.counties.aea, region="GEO_ID")
co.map$id <- str_replace(co.map$id, "0500000US", "")

co.map <- merge(co.map, county.data, by="id")

#========================================================
# Step 3: Generate the choropleth maps
#========================================================

# ----------------FIGURE 1--------------------------------
library(ineq)
fe <- read.csv("maps_matched.csv")
gini_vc_m = round(ineq(fe$vc_count, type="Gini", na.rm=TRUE), 3)
gini_ks_m = round(ineq(fe$num_campaigns, type="Gini", na.rm=TRUE),3)
gini_prop_m = round(ineq(fe$prop, type="Gini", na.rm=TRUE),3)

hhi_vc_m = round(conc(fe$vc_count, type="Herfindahl", na.rm=TRUE),3)
hhi_ks_m = round(conc(fe$num_campaigns, type="Herfindahl", na.rm=TRUE),3)
hhi_prop_m = round(conc(fe$prop, type="Herfindahl", na.rm=TRUE),3)

#Number Kickstarter Campaigns-----------------------------
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= num_campaigns),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Blues", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom", legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="KS Campaigns")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("Fig1a.png",
       p2,
       height=6,
       width=6,
       dpi=300)
       
       
#Number Venture Capital Rounds-----------------------------
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill=vc_count),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Blues", labels=c("0", "1-9", "10-99", "100-999", "1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom",legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="VC Rounds")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_vc_m,"\nHHI=",hhi_vc_m))
p2

ggsave("Fig1b.png",
       p2,
       height=6,
       width=6,
       dpi=300)
       
       
#Proportion Kickstarter to Venture Capital-----------------------------
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill=prop),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(type="seq", palette="Oranges", na.value="grey79", labels=c("0 < Ratio < 0.5", "0.5 < Ratio < 0.9","0.9 < Ratio < 1.1", "1.1 < Ratio < 2 ", "2 < Ratio")
)
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom",legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_prop_m,"\nHHI=",hhi_prop_m))
p2

ggsave("Fig1c.png",
       p2,
       height=8,
       width=8,
       dpi=300)
ggsave("Fig1c.pdf",
       p2,
       height=8,
       width=8,
       dpi=300)       

# FUNDING PER IDEA

library(ineq)
fe <- read.csv("maps_matched.csv")
gini_vc_m = round(ineq(fe$vc_count, type="Gini", na.rm=TRUE), 3)
gini_ks_m = round(ineq(fe$num_campaigns, type="Gini", na.rm=TRUE),3)
gini_prop_m = round(ineq(fe$prop, type="Gini", na.rm=TRUE),3)

hhi_vc_m = round(conc(fe$vc_count, type="Herfindahl", na.rm=TRUE),3)
hhi_ks_m = round(conc(fe$num_campaigns, type="Herfindahl", na.rm=TRUE),3)
hhi_prop_m = round(conc(fe$prop, type="Herfindahl", na.rm=TRUE),3)

#Kickstarter-----------------------------
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= (cf_patents)),
                   color="white",
                   size=0.2)
0, 100, 1000, 10000, 10000000
p2 <- p1 + scale_fill_brewer(palette="Blues", labels=c("0", "$100-999", "$1,000-9,999", "$10,000-99,999","$100,000-999,999", "1,000,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom") + labs(fill="Kickstarter Funding per Idea,\n2009-2015")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("FigCF.png",
       p2,
       height=8,
       width=12,
       dpi=300)
       
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= (vc_patents)),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Blues", labels=c("0", "$100-999", "$1,000-9,999", "$10,000-99,999","$100,000-999,999", "1,000,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom") + labs(fill="VC Funding per Idea,\n2009-2015")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("FigVC.png",
       p2,
       height=8,
       width=12,
       dpi=300)

# ----------------Patenting & Citations-------------------------------- 
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= cf_patents),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(type="seq", na.value="grey75",  palette="OrRd", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom", legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="KS Patents")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("FigR1.png",
       p2,
       height=6,
       width=6,
       dpi=300)
       
p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= vc_patents),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(type="seq", na.value="grey75", palette="OrRd", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom", legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="VC Patents")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("FigR2.png",
       p2,
       height=6,
       width=6,
       dpi=300)

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= cf_cites),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(type="seq", na.value="grey75", palette="RdPu", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom", legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="KS Patent Citations")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("FigR3.png",
       p2,
       height=6,
       width=6,
       dpi=300)
       
p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= vc_cites),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(type="seq", na.value="grey79", palette="RdPu", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom", legend.text=element_text(size=12), legend.title=element_text(size=12)) + labs(fill="VC Patent Citations")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_m,"\nHHI=",hhi_ks_m))
p2

ggsave("FigR4.png",
       p2,
       height=6,
       width=6,
       dpi=300)
       
# --------------------------------------------------------------------
# Supplement -- CUMULATIVE DISTRIBUTIONS
# --------------------------------------------------------------------

state.data <- read.csv("state-data-statabs-2012.csv", header=TRUE)

county.names <- read.csv("fips-by-state.csv", header=TRUE)
county.names$fips  <- as.character(county.names$fips)
ind <- county.names$fips<10000
county.names$id[ind] <- paste("0", county.names$id[ind], sep="")
county.names$id[county.names$id=="00"] <- "0"

county.data <- read.csv("maps_data_final.csv", header=TRUE, sep=",")
colnames(county.data) = c("fips", "amount", "num_campaigns","num_successful","instrument_successful","successful_100","vc_count","vc_amount", "total_ks_amount", "total_vc_amount", "prop")
county.data = merge(county.names, county.data, by.x="fips", by.y="fips", all.x=TRUE)
county.data$fips <- as.numeric(county.data$fips)
ind <- county.data$fips<10000
county.data$id <- as.character(county.data$fips)
county.data$id[ind] <- paste("0", county.data$id[ind], sep="")
county.data$id[county.data$id=="00"] <- "0"

# replace NA data with zeros for counties with no capital
county.data$amount <- ifelse(is.na(county.data$amount), 0, county.data$amount)
county.data$num_campaigns <- ifelse(is.na(county.data$num_campaigns), 0, county.data$num_campaigns)
county.data$num_successful <- ifelse(is.na(county.data$num_successful), 0, county.data$num_successful)
county.data$instrument_successful <- ifelse(is.na(county.data$instrument_successful), 0, county.data$instrument_successful)
county.data$successful_100 <- ifelse(is.na(county.data$successful_100), 0, county.data$successful_100)
county.data$vc_count <- ifelse(is.na(county.data$vc_count), 0, county.data$vc_count)
county.data$vc_amount <- ifelse(is.na(county.data$vc_amount), 0, county.data$vc_amount)
county.data$total_ks_amount <- ifelse(is.na(county.data$total_ks_amount), 0, county.data$total_ks_amount)
county.data$total_vc_amount <- ifelse(is.na(county.data$total_vc_amount), 0, county.data$total_vc_amount)
county.data$prop <- ifelse(is.na(county.data$prop), 0, county.data$prop)

ind <- match(county.data$fips, county.names$fips)
county.data$name <- county.names$name[ind]
county.data$state <- county.names$state[ind]

ind <- match(state.data$State.Abbr, county.data$state)
county.data$state[ind] <- state.data$State.Abbr

## Add state names as levels of county name, so states have FIPS too
levels(county.data$name) <- c(levels(county.data$name), levels(state.data$State))
county.data$name[ind] <- state.data$State

### Add census region. Don't call the variable "region" because that's
### already reserved by the map object
ind <- match(county.data$state, state.data$State.Abbr)
county.data$census.region <- state.data$Region[ind]

library(Hmisc)
county.data$num_campaigns <- cut2(county.data$num_campaigns,
                             cuts = c(0, 1, 10, 100, 1000))
county.data$num_successful <- cut2(county.data$num_successful,
                             cuts = c(0, 1, 10, 100, 1000))
county.data$successful_100 <- cut2(county.data$successful_100,
                             cuts = c(0, 1, 10, 100, 1000))                             
county.data$vc_count <- cut2(county.data$vc_count,
                             cuts = c(0, 1, 10, 100, 1000))

county.data$prop <- cut2(county.data$prop,
                             cuts = c(0, .1, 1, 10, 100))     
                                                                               
co.map <- fortify(us.counties.aea, region="GEO_ID")
co.map$id <- str_replace(co.map$id, "0500000US", "")

co.map <- merge(co.map, county.data, by="id")

#========================================================
# Step 3: Generate the choropleth maps
#========================================================

library(ineq)
fe <- read.csv("maps_data_final.csv")
gini_vc = round(ineq(fe$vc_count, type="Gini", na.rm=TRUE), 3)
gini_ks = round(ineq(fe$num_campaigns, type="Gini", na.rm=TRUE),3)
gini_ks_succ = round(ineq(fe$num_successful, type="Gini", na.rm=TRUE),3)
gini_ks_succ_100 = round(ineq(fe$successful_100, type="Gini", na.rm=TRUE),3)

hhi_vc = round(conc(fe$vc_count, type="Herfindahl", na.rm=TRUE),3)
hhi_ks = round(conc(fe$num_campaigns, type="Herfindahl", na.rm=TRUE),3)
hhi_ks_succ = round(conc(fe$num_successful, type="Herfindahl", na.rm=TRUE),3)
hhi_ks_succ_100 = round(conc(fe$successful_100, type="Herfindahl", na.rm=TRUE),3)

#Number Kickstarter Campaigns-----------------------------
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= num_campaigns),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Purples", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom") + labs(fill="Kickstarter Campaigns, 2009-2015")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks,"\nHHI=",hhi_ks))
p2

ggsave("FigS3.png",
       p2,
       height=8,
       width=12,
       dpi=300)
       
p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= num_successful),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Purples", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom") + labs(fill="Successful Kickstarter\nCampaigns, 2009-2015")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label =paste("Gini=",gini_ks_succ,"\nHHI=",hhi_ks_succ))
p2

ggsave("FigS4.png",
       p2,
       height=8,
       width=12,
       dpi=300)

p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill= successful_100),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Purples", labels=c("0", "1-9", "10-99", "100-999","1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom") + labs(fill="Successful Kickstarter\nCampaigns>$100,000, 2009-2015")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label=paste("Gini=",gini_ks_succ_100,"\nHHI=",hhi_ks_succ_100))
p2

ggsave("FigS5.png",
       p2,
       height=8,
       width=12,
       dpi=300)
       
#Number Venture Capital Rounds-----------------------------
p <- ggplot(data=co.map, aes(x=long, y=lat, group=group))
       
p1 <- p + geom_map(data=co.map,
                   map = co.map,
                   aes(map_id=id,
                       x=long,
                       y=lat,
                       group=group,
                       fill=vc_count),
                   color="white",
                   size=0.2)

p2 <- p1 + scale_fill_brewer(palette="Blues", labels=c("0", "1-9", "10-99", "100-999", "1,000+"))
p2 <- p2 + coord_equal()
p2 <- p2 + theme_map()
p2 <- p2 + theme(legend.position="bottom") + labs(fill="VC seed & early stage\nfunding rounds, 2009-2015")
p2 <- p2 + ggtitle("") #+ annotate("text", x = 0.6*max(co.map$long), y = 0.8*max(co.map$lat), size=5, color="black", label=paste("Gini=",gini_vc,"\nHHI=",hhi_vc))
p2

ggsave("FigS6.png",
       p2,
       height=8,
       width=12,
       dpi=300)
       
